---
title: 'Read me for replication materials'
subtitle: 'Dür, Hee & Huber (2025) A Fair Deal: Inequity Aversion and Individual Attitudes toward Trade Agreements, *Review of International Organizations*'
author: Andreas Dür, Stefan Hee, and Robert A. Huber
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
rm(list=ls())
```

This document guides through the replication for the following publication: Dür, Hee & Huber (2025) A Fair Deal: Inequity Aversion and Individual Attitudes toward Trade Agreements, *Review of International Organizations*. Thank you for your interest in our manuscript and the underlying data.

# How to proceed

All scripts required to replicate the results in `R` can be run from the file `master.R`. This file includes all analyses with the survey data necessary to replicate the paper. 

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/TBTCPB>, please open the file `RIO_replication_material.Rproj`. You need to have installed `R` (version 4.3x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `master.R` from the folder `r_code` within the project.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload, warning=FALSE, message=FALSE, results='hide'}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("tidyverse",
          "marginaleffects",
          "irr",
          "texreg",
          "xtable")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

# Set wd with here() package

here::i_am("RIO_replication_materials.Rproj")
```


```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```


# List of initial files

The files should cover the following:

```{r}
#List of files
list.files()
list.files(path = "./data/")
list.files(path = "./r_code/")
```

# Analysis

This script `master.R` runs all individual manuscripts and thereby replicates analyses shown in the paper and the appendix. The script `study1.R` analyses the conjoint experiment based on the Polish and Spanish survey data. `study2.R` analyses the list experiment and conjoint experiment based on the United Kingdom data. `open_questions.R` provides the information on open text field questions in both surveys and provides the coding discussed in the paper. `prepTex.R` loads a function to extract regression models with clustered standard errors within the `texreg` package. 

```{r, include=T, warning=FALSE, message=FALSE, results='hide', fig.show='hide'}
run_start <- Sys.time()

source("r_code/master.R", echo = T)

```

```{r}
run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# List of created files

The replication files should create the following:

```{r}
#List of files
list.files("./figures/")
list.files("./tables/")
```

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
